home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 24
/
Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso
/
Aminet
/
comm
/
mail
/
Mutt089src.lha
/
Mutt-0.89i-AMIGA
/
src
/
attach.c
< prev
next >
Wrap
C/C++ Source or Header
|
1998-01-28
|
15KB
|
657 lines
/*
* Copyright (C) 1996-8 Michael R. Elkins <me@cs.hmc.edu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "mutt.h"
#include "mutt_menu.h"
#include "mutt_curses.h"
#include "keymap.h"
#include "send.h" /* for mutt_edit_file(), in curs_lib.c? */
#include "rfc1524.h"
#include "mime.h"
#include "state.h"
#include "parse.h"
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
/*
* Currently, this only works for send mode, as it assumes that the
* BODY->filename actually contains the information. I'm not sure
* we want to deal with editing attachments we've already received,
* so this should be ok.
*
* Returns 1 if editor found, 0 if not (useful to tell calling menu to
* redraw)
*/
int mutt_edit_attachment (BODY *a, int opt)
{
char type[STRING];
char command[STRING];
rfc1524_entry *entry = rfc1524_new_entry ();
snprintf (type, sizeof (type), "%s/%s", TYPE (a->type), a->subtype);
if (rfc1524_mailcap_lookup (a, type, entry, M_EDIT))
{
if (entry->editcommand)
{
char newfile[_POSIX_PATH_MAX] = "";
strfcpy (command, entry->editcommand, sizeof (command));
if (rfc1524_expand_filename (entry->nametemplate,
a->filename, newfile, sizeof (newfile)))
{
dprint(1, (debugfile, "oldfile: %s\t newfile: %s\n",
a->filename, newfile));
if (!mutt_rename_file (a->filename, newfile))
{
if (!mutt_yesorno ("Can't match nametemplate, continue?", 1))
return 0;
}
else
{
safe_free ((void **) &a->filename);
a->filename = safe_strdup (newfile);
}
}
if (rfc1524_expand_command (a, a->filename, type,
command, sizeof (command)))
{
/* For now, editing requires a file, no piping */
mutt_error ("Mailcap Edit entry requires %%s");
}
else
{
endwin ();
mutt_system (command);
}
}
}
else if (a->type == TYPETEXT)
{
/* On text, default to editor */
mutt_edit_file (strcmp ("builtin", Editor) == 0 ? Visual : Editor,
a->filename);
}
else
{
rfc1524_free_entry (&entry);
mutt_error ("No mailcap edit entry for %s",type);
return 0;
}
rfc1524_free_entry (&entry);
return 1;
}
int mutt_is_autoview (char *type)
{
LIST *t = AutoViewList;
int i;
while (t)
{
i = strlen (t->data) - 1;
if ((i > 0 && t->data[i-1] == '/' && t->data[i] == '*' &&
strncasecmp (type, t->data, i) == 0) ||
strcasecmp (type, t->data) == 0)
return 1;
t = t->next;
}
return 0;
}
/* returns 0 on error, 1 on success, 2 if we need to toggle OPTWEED */
int mutt_view_attachment (FILE *fp, BODY *a, int flag)
{
char tempfile[_POSIX_PATH_MAX] = "";
char pagerfile[_POSIX_PATH_MAX] = "";
int is_message;
int use_mailcap;
int use_pipe = 0;
int use_pager = 1;
char type[STRING];
char command[STRING];
char descrip[STRING];
rfc1524_entry *entry = NULL;
int rc = 0;
is_message = (a->type == TYPEMESSAGE && a->subtype &&
(!strcasecmp (a->subtype,"rfc822") ||
!strcasecmp (a->subtype, "news")));
use_mailcap = (flag == M_MAILCAP ||
(flag == M_REGULAR && mutt_needs_mailcap (a)));
snprintf (type, sizeof (type), "%s/%s", TYPE (a->type), a->subtype);
if (use_mailcap)
{
entry = rfc1524_new_entry ();
if (!rfc1524_mailcap_lookup (a, type, entry, 0))
{
if (flag == M_REGULAR)
{
/* fallback to view as text */
rfc1524_free_entry (&entry);
mutt_error ("No matching mailcap entry found. Viewing as text.");
flag = M_AS_TEXT;
use_mailcap = 0;
}
else
goto return_error;
}
}
if (use_mailcap)
{
if (!entry->command)
{
mutt_error ("MIME type not defined. Cannot view attachment.");
goto return_error;
}
strfcpy (command, entry->command, sizeof (command));
if (rfc1524_expand_filename (entry->nametemplate, a->filename,
tempfile, sizeof (tempfile)))
{
if (fp == NULL)
{
/* send case: the file is already there */
if (!mutt_rename_file (a->filename, tempfile))
{
if (mutt_yesorno ("Can't match nametemplate, continue?", 1) == M_YES)
strfcpy (tempfile, a->filename, sizeof (tempfile));
else
goto return_error;
}
else
{
safe_free ((void **) &a->filename);
a->filename = safe_strdup (tempfile);
}
}
}
else if (fp == NULL) /* send case */
strfcpy (tempfile, a->filename, sizeof (tempfile));
if (fp)
{
/* recv case: we need to save the attachment to a file */
if (mutt_save_attachment (fp, a, tempfile, 0) == -1)
goto return_error;
}
use_pipe = rfc1524_expand_command (a, tempfile, type,
command, sizeof (command));
use_pager = entry->copiousoutput;
}
if (use_pager)
{
if (fp && !use_mailcap && a->filename)
{
/* recv case */
strfcpy (pagerfile, a->filename, sizeof (pagerfile));
mutt_adv_mktemp (pagerfile);
}
else
mutt_mktemp (pagerfile);
}
if (use_mailcap)
{
pid_t thepid = 0;
FILE *pagerfp = NULL;
FILE *tempfp = NULL;
FILE *filter_in;
FILE *filter_out;
if (!use_pager)
endwin ();
if (use_pager || use_pipe)
{
if (use_pager && ((pagerfp = safe_fopen (pagerfile, "w")) == NULL))
{
mutt_perror ("fopen");
goto return_error;
}
if (use_pipe && ((tempfp = fopen (tempfile, "r")) == NULL))
{
if (pagerfp)
fclose (pagerfp);
mutt_perror ("fopen");
goto return_error;
}
if ((thepid = mutt_create_filter (command, use_pipe ? &filter_in : NULL,
use_pager ? &filter_out : NULL, NULL)) == -1)
{
if (pagerfp)
fclose (pagerfp);
if (tempfp)
fclose (tempfp);
mutt_error ("Cannot create filter");
goto return_error;
}
if (use_pipe)
{
mutt_copy_stream (tempfp, filter_in);
fclose (tempfp);
fclose (filter_in);
}
if (use_pager)
{
mutt_copy_stream (filter_out, pagerfp);
fclose (filter_out);
fclose (pagerfp);
if (a->description)
snprintf (descrip, sizeof (descrip),
"---Command: %-20.20s Description: %s",
command, a->description);
else
snprintf (descrip, sizeof (descrip),
"---Command: %-30.30s Attachment: %s", command, type);
}
if ((mutt_wait_filter (thepid) || (entry->needsterminal &&
option (OPTWAITKEY))) && !use_pager)
mutt_any_key_to_continue (NULL);
}
else
{
/* interactive command */
if (mutt_system (command) || (entry->needsterminal &&
option (OPTWAITKEY)))
mutt_any_key_to_continue (NULL);
}
}
else
{
/* Don't use mailcap; the attachment is viewed in the pager */
if (flag == M_AS_TEXT)
{
/* just let me see the raw data */
if (mutt_save_attachment (fp, a, pagerfile, 0))
goto return_error;
}
else
{
/* Use built-in handler */
set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part"
* message in case of error */
if (mutt_decode_save_attachment (fp, a, pagerfile, 1, 0))
{
unset_option (OPTVIEWATTACH);
goto return_error;
}
unset_option (OPTVIEWATTACH);
}
if (a->description)
strfcpy (descrip, a->description, sizeof (descrip));
else
snprintf (descrip, sizeof (descrip), "---Attachment: %s", type);
}
/* We only reach this point if there have been no errors */
rc = 1;
if (use_pager)
{
/* we have no other way but to do